package com.sql;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

/**
 *
 * @author Khaled Lela
 */
public class CheckLogin {

    public CheckLogin() {
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
            DriverManager.getConnection("jdbc:derby:BreadDB;");
        } catch (Exception e) {
        }
    }

    //Loging Check >>>!!!
    public CheckLogin(String user, String pass) {
        this.userName = user;
        this.passWord = pass;
    }

    public boolean isAuthorized() {
        try {
            con = DriverManager.getConnection("jdbc:derby:BreadDB;");
            statement = con.createStatement();
            ResultSet rs = statement.executeQuery("select * from login where username='" + userName + "'"
                    + " and password='" + passWord + "'");
            if (rs.next()) {
                isAthor = true;
            } else {
                isAthor = false;
            }
        } catch (Exception e) {
        } finally {
            shutdownCon();
        }
        return isAthor;
    }

    //shutdown database connection
    private void shutdownCon() {
        try {
            DriverManager.getConnection("jdbc:derby:;shutdown=true");
        } catch (Exception ex) {
        }
    }
    private Connection con;
    private Statement statement;
    private String userName, passWord;
    private boolean isAthor;
}
